package com.tg.data.helper;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.appbase.custom.base.CarServerData;
import com.appbase.custom.base.ServiceInfo;
import com.appbase.custom.constant.DeviceStateConstants;
import com.tange.base.toolkit.StringUtils;
import com.tg.data.bean.DeviceItem;

public class DeviceItemHelper {
    public static CarServerData getCarServer(DeviceItem deviceItem) {
        return deviceItem != null ? deviceItem.getCarServerData() : null;
    }

    public static boolean isSuperAiService(DeviceItem deviceItem){
        return deviceItem != null && deviceItem.should_show_super_ai;
    }

    public static ServiceInfo getServer(DeviceItem deviceItem) {
        return deviceItem != null ? deviceItem.getServer() : null;
    }


    public static boolean isBatteryDevice(DeviceItem deviceItem) {
        return deviceItem != null && deviceItem.isBatteryDevice();
    }


    public static boolean isCar(DeviceItem deviceItem) {
        return deviceItem != null && deviceItem.isCar();
    }

    public static boolean isBirdFeeder(DeviceItem deviceItem){
        return deviceItem != null && deviceItem.isBirdFeeder();
    }
    public static boolean isWifiDevice(DeviceItem deviceItem) {
        return deviceItem != null && deviceItem.isWifiDevice();
    }

    public static boolean isLanDevice(DeviceItem deviceItem) {
        return deviceItem != null && DeviceTypeHelper.isLanDevice(deviceItem.device_type);
    }

    public static boolean is4GDevice(DeviceItem deviceItem) {
        return deviceItem != null && deviceItem.is4GDevice();
    }
    public static boolean is4GDeviceWithSimCard(DeviceItem deviceItem) {
        return deviceItem != null && deviceItem.is4GDevice()&& TextUtils.equals(deviceItem.getConnectWay(), "sim") ;
    }

    public static boolean is4GExpired(DeviceItem deviceItem) {
        return is4GExpired(deviceItem, false);
    }

    public static boolean is4GExpired(DeviceItem deviceItem, boolean neglectConnectWay) {
        if (neglectConnectWay){
            return is4GDevice(deviceItem) && deviceItem.getSimServerData() == null && !DeviceItemHelper.isCar(deviceItem);
        }
        return is4GDevice(deviceItem) && deviceItem.getSimServerData() == null && TextUtils.equals(deviceItem.getConnectWay(), "sim") && !DeviceItemHelper.isCar(deviceItem);
    }

    public static boolean hasServe(DeviceItem deviceItem) {
        if (isBirdFeeder(deviceItem)){
            return deviceItem.ai_server_data != null;
        }
        return isWifiDevice(deviceItem) && getServer(deviceItem) != null || isCar(deviceItem) && getCarServer(deviceItem) != null || is4GDevice(deviceItem) && !is4GExpired(deviceItem) && !DeviceItemHelper.isCar(deviceItem);
    }

    public static boolean isCarPrimary(DeviceItem deviceItem) {
        return isCar(deviceItem) && deviceItem.isCarPrimary();
    }

    public static boolean isStandard(DeviceItem deviceItem) {
        return isCar(deviceItem) && deviceItem.isStandard();
    }

    public static boolean isNoScardServer(DeviceItem deviceItem) {
        ServiceInfo serviceInfo = getServer(deviceItem);
        return serviceInfo == null ? false : serviceInfo.built_in;
    }

    public static boolean isOnline(DeviceItem deviceItem) {
        return deviceItem != null && deviceItem.is_online == DeviceStateConstants.DEVICE_ONLINE;
    }

    public static boolean isShare(DeviceItem deviceItem) {
        return deviceItem != null && deviceItem.isShare();
    }
    public static boolean is4GSupportCloud(DeviceItem deviceItem){
        return deviceItem != null && is4GDevice(deviceItem)&&deviceItem.is_directional;
    }

    public static boolean is4GHasCloud(DeviceItem deviceItem){
        return deviceItem != null && is4GDevice(deviceItem)&&is4GSupportCloud(deviceItem)&&(deviceItem.getSimServerData()!=null&&deviceItem.getSimServerData().isSimCloud()||isCar(deviceItem)&&!isCarPrimary(deviceItem));
    }

    public static boolean isLockBell(DeviceItem deviceItem) {
        return deviceItem != null && deviceItem.device_type != null && DeviceTypeHelper.isLockBell(deviceItem.device_type);
    }

    public static boolean isContains(String attr, String key, String val){
        if (StringUtils.isEmpty(attr)){
            return false;
        }
        JSONObject jsonObject = JSONObject.parseObject(attr);
        if (jsonObject != null){
            String power = jsonObject.getString(key);
            return StringUtils.equalsIgnoreCase(power, val);
        }
        return false;
    }

}
